package infrastructure;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

public class Utils {

	/**
	 * @param args
	 */
	public static String concatenateString(String[] args) {
		String result= "";
		for(String s : args){
			result += s + " ";
		}

		return result;
	}
	
	public static Object[] readParameters(String filePath){
		// TODO: Read from file to do it more dinamically
		Object [] params = new Object[4];
		// Momentum
		params[0] = new Double(0.2);
		// Learning Rate
		params[1] = new Double(0.15);
		// Epochs
		params[2] = new Integer(1);
		// hidden Layers
		params[3] = "500";
		
		return params;
	}
	
	/**
	 * Reads all the lines of the result text file and returns them in a List.
	 * @param filepath The path of the file to read.
	 * @param flagToStartReading Indicates the string, that the reader should lookup to start reading the file. If its null, then read the whole file.
	 * @return The List of lines of the result text file
	 * @throws Exception 
	 */
	public static List<String> readFile(String filepath, String flagToStartReading) throws Exception{
		File file = new File(filepath);
		List<String> result = new ArrayList<String>();
		// This variables indicates if the flag was founded. If it was founded, then the file can be read.
		boolean hasFlagBeingFounded= false;
		// If it is null, the read the whole file.
		if (flagToStartReading == null)
			hasFlagBeingFounded = true;
		
		if (!file.exists()) {
			throw new Exception("File does not exist");
		}
		else {
			FileReader reader = new FileReader(file);
			BufferedReader buffer = new BufferedReader(reader);
			
			// Read First Line
			String line = buffer.readLine();
			while (line != null) {
				if (hasFlagBeingFounded) {
					if (!line.isEmpty())
						result.add(line);
				}
				else
					if (line.contains(flagToStartReading)){
						hasFlagBeingFounded = true;
					}
				
				// Read next line
				line = buffer.readLine();
			}
			buffer.close();
			reader.close();			
		}
		
		return result;
	}

}
